<?php


    $paperId=$_GET['paper_id'];

    //connect to the database
    $conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
     mysql_select_db('mydb', $conn) or die(mysql_error());

    //arrange the versions and get the latest version
    $result= mysql_query("SELECT `version_id` FROM `version` WHERE `paper_id`= $paperId ORDER BY version_id DESC");
    $row = mysql_fetch_assoc($result);
    $LatestV = $row['version_id'];
    
    //get the path of the latest version
    $query = "SELECT path FROM `version` Where version.paper_id = $paperId AND version.version_id=$LatestV;";
    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);
    $path = $row ['path'];

        //download the pdf
     header( "Content-type: application/pdf");
     header("Content-Disposition: attachment; filename= $path");
   
     readfile($path);
     

?>